package com.sskj.lib;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.hjq.toast.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.sskj.common.base.AppManager;
import com.sskj.common.base.HttpData;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.PermissionTipUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.bean.DownMusicBean;
import com.sskj.lib.http.JsonCallBack;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;

public class MusicService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        getMusic();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void getMusic() {
        OkGo.<HttpData<MusicBean>>post(BaseHttpConfig.BASE_URL + "/Home/Users/getBgMusic")
                .tag(this)
                .execute(new JsonCallBack<HttpData<MusicBean>>() {
                    @Override
                    public void onHTTPSuccess(HttpData<MusicBean> data) {
                        if (data.getData() == null) {
                            return;
                        }
                        new RxPermissions(AppManager.getAppManager().currentActivity()).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                                .subscribe(isGrant -> {
                                    if (isGrant) {
                                        OkGo.<File>get(data.getData().getAddr_muisc()).execute(new FileCallback() {
                                            @Override
                                            public void onSuccess(Response<File> response) {
                                                SPUtil.put(SPConfig.MUSIC_PATH, response.body().getPath());
                                            }

                                            @Override
                                            public void onStart(Request<File, ? extends Request> request) {
                                                super.onStart(request);
                                            }

                                            @Override
                                            public void onFinish() {
                                                super.onFinish();
                                                RxBus.getDefault().post(new DownMusicBean());
                                            }

                                            @Override
                                            public void onError(Response<File> response) {
                                                super.onError(response);
                                                ToastUtils.show("音乐文件下载失败");
                                            }
                                        });
                                    } else {
                                        PermissionTipUtil.tip(MusicService.this, "存储");
                                    }
                                });
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        stopSelf();
                    }
                });
    }

    private class MusicBean {

        private String addr_muisc;

        public String getAddr_muisc() {
            return addr_muisc == null ? "" : addr_muisc;
        }

        public void setAddr_muisc(String addr_muisc) {
            this.addr_muisc = addr_muisc;
        }
    }

}
